package com.quwan.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quwan.common.result.PageResult;
import com.quwan.config.yml.OssConfig;
import com.quwan.entity.TBanner;
import com.quwan.entity.TMemberDynamicFile;
import com.quwan.manager.controller.dto.MemberDynamicDTO;
import com.quwan.manager.service.ManagerMemberDynamicService;
import com.quwan.mapper.TMemberDynamicFileMapper;
import com.quwan.mapper.TMemberDynamicMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author laozhang
 * @date 2021/11/2 10:49
 */
@Service
@RequiredArgsConstructor
public class ManagerMemberDynamicServiceImpl implements ManagerMemberDynamicService {
    private final TMemberDynamicMapper memberDynamicMapper;
    private final TMemberDynamicFileMapper memberDynamicFileMapper;
    private final OssConfig ossConfig;

    @Override
    public PageResult<MemberDynamicDTO> getManagerMemberDynamicList(int pageNo, int pageSize, String mId, String name, String beginTime, String endTime) {
        Page<MemberDynamicDTO> resultMember =  memberDynamicMapper.selectMemberDynamicListAfter(new Page<>(pageNo, pageSize),mId,name,beginTime,endTime);
        List<MemberDynamicDTO> resList = resultMember.getRecords();
        /* 开始遍历 */
        for (MemberDynamicDTO ban : resList) {
            if(ban.getItem()!= null && ban.getItem().size()>0){
                List<TMemberDynamicFile> files = ban.getItem();
                for (TMemberDynamicFile file : files) {
                    if(file.getUrl()!= null && file.getUrl().length()>0){
                        file.setUrl(ossConfig.getHostname()+"/"+file.getUrl());
                    }
                }
            }
        }

        PageResult<MemberDynamicDTO> result = new PageResult<MemberDynamicDTO>()
                .setCurrent(resultMember.getCurrent())
                .setPages(resultMember.getPages())
                .setSize(resultMember.getSize())
                .setTotal(resultMember.getTotal())
                .setRecords(resList);
        return result;
    }

    @Override
    public boolean delDynamic(String id) {
        memberDynamicFileMapper.delete(
                new LambdaQueryWrapper<TMemberDynamicFile>()
                 .eq(TMemberDynamicFile::getDynamicId,id));
        memberDynamicMapper.deleteById(id);
        return true;
    }
}
